package structures.review.link;

import java.util.Objects;

public class CircleLinkListDemo {
    public static void main(String[] args) {
        CircleNodeList circleNodeList = new CircleNodeList();
        circleNodeList.add(1);
        circleNodeList.add(2);
        circleNodeList.add(3);
        circleNodeList.add(4);
        circleNodeList.add(5);
        circleNodeList.add(6);
        circleNodeList.add(7);
        circleNodeList.list();
    }
}

class CircleNodeList {
    private CircleNode head;

    public CircleNodeList() {
    }

    public void add(int data) {
        CircleNode circleNode = new CircleNode(data);
        if (head == null) {
            head = circleNode;
            return;
        }
        CircleNode curNode = head;
        while (curNode.getNext() != null &&
                curNode.getNext() != head) {
            curNode = curNode.getNext();
        }
        curNode.setNext(circleNode);
        circleNode.setNext(head);
    }


    public void list() {
        if (head == null) {
            return;
        }
        CircleNode curNode = head;
        while (curNode.getNext() != null &&
                curNode.getNext() != head) {
            System.out.println(curNode);
            curNode = curNode.getNext();
        }
        System.out.println(curNode);
    }
}

class CircleNode {
    private int id;
    private CircleNode next;

    public CircleNode(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public CircleNode getNext() {
        return next;
    }

    public void setNext(CircleNode next) {
        this.next = next;
    }

    @Override
    public String toString() {
        return "CircleNode{" +
                "id=" + id +
                '}';
    }
}
